﻿
using System;
using System.Data;
using System.Text;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Openfree.Framework.DataAccess;

public partial class PMSWeb_MeetAddress : PMSBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string flag = Request["flag"];
            string curdate = Request["curdate"];
            if (string.IsNullOrEmpty(flag))
            {
                CurDateTime = DateTime.Now;
            }
            else if (flag == "pre")
            {
                CurDateTime = Convert.ToDateTime(curdate);
                SetPreWeek();
            }
            else if (flag == "next")
            {
                CurDateTime = Convert.ToDateTime(curdate);
                SetNextWeek();
            } 
            InitData();
            L_preweek.NavigateUrl = "MeetAddress.aspx?flag=pre&curdate=" + CurDateTime.ToString("yyyy-MM-dd");
            L_nextweek.NavigateUrl = "MeetAddress.aspx?flag=next&curdate=" + CurDateTime.ToString("yyyy-MM-dd");
        }
    }
    SqlCodeConfig codeConfig;
    private DateTime m_CurDateTime = DateTime.Now;
    public DateTime CurDateTime
    {
        get
        {
            return m_CurDateTime;
        }
        set
        {
            m_CurDateTime = value;
        }
    }
    private DataSet m_ds;
    private void InitData()
    { 
        hfcurstdate.Value = CurDateTime.ToString("yyyy-MM-dd");
        string stdate = GetMondayDate().ToString("yyyy-MM-dd");
        string enddate = GetSundayDate().ToString("yyyy-MM-dd");
        datediff.InnerText = "(" + stdate + "   到   " + enddate + ")";
        string sql = "select * from PMS_Meet where MeetTime >='" + stdate + "' and MeetTime<='" + enddate + "'";
        m_ds = PMSAccess.GetDataSet(this.Connection, sql);

        DataTable dt = new DataTable();
        dt.Columns.Add("address");
        dt.Columns.Add("addresscode");
        dt.Columns.Add("one");
        dt.Columns.Add("two");
        dt.Columns.Add("three");
        dt.Columns.Add("four");
        dt.Columns.Add("five");
        dt.Columns.Add("six");
        dt.Columns.Add("seven");

        codeConfig = new SqlCodeConfig(this.Connection);
        DataSet ds = codeConfig.LoadDataByCategory(PMSCodeCategory.PMS_MEETAD);
        foreach(DataRow dr in ds.Tables[0].Rows){
            DataRow row = dt.NewRow();
            row["address"] = dr["CodeName"];
            row["addresscode"] = dr["CodeValue"];
            dt.Rows.Add(row);
        } 
        gridView.DataSource = dt;
        gridView.DataBind();
    }
    private string GetTimeString(string ad,int i)
    {
        if (m_ds != null && m_ds.Tables.Count > 0 && m_ds.Tables[0].Rows.Count > 0)
        { 
            string date = CurDateTime.AddDays(i-1).ToString("yyyy-MM-dd");
            DataRow[] rows = m_ds.Tables[0].Select("MeetTime='" + date + "' and MeetAddress='" + ad + "'");
            StringBuilder res = new StringBuilder();
            foreach(DataRow dr in rows)
            {
                res.Append("<a href='MeetManager.aspx?MeetID=" + dr["MeetID"].ToString() + "' target='_blank'>")
                    .Append(dr["StartTime"].ToString()).Append(" - ").Append(dr["EndTime"].ToString())
                    .Append("</a>")
                    .Append("<br/>");
            }
            return res.ToString();
        }
        return "";
    }
    protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string code = ((HiddenField)e.Row.Cells[0].Controls[1]).Value;
            e.Row.Cells[1].Text = GetTimeString(code, 1);
            e.Row.Cells[2].Text = GetTimeString(code, 2);
            e.Row.Cells[3].Text = GetTimeString(code, 3);
            e.Row.Cells[4].Text = GetTimeString(code, 4);
            e.Row.Cells[5].Text = GetTimeString(code, 5);
            e.Row.Cells[6].Text = GetTimeString(code, 6);
            e.Row.Cells[7].Text = GetTimeString(code, 7);
        }
    }

    public void SetPreWeek()
    {
        CurDateTime = CurDateTime.AddDays(-7);
    }
    public void SetNextWeek()
    {
        CurDateTime = CurDateTime.AddDays(7);
    }
    public DateTime GetMondayDate()
    {
        int i = CurDateTime.DayOfWeek - DayOfWeek.Monday; if (i == -1) i = 6;
        // i值 > = 0 ，因为枚举原因，Sunday排在最前，此时Sunday-Monday=-1，必须+7=6。         
        TimeSpan ts = new TimeSpan(i, 0, 0, 0); return CurDateTime.Subtract(ts);
    }
    public DateTime GetSundayDate()
    {
        int i = CurDateTime.DayOfWeek - DayOfWeek.Sunday; if (i != 0) i = 7 - i;
        // 因为枚举原因，Sunday排在最前，相减间隔要被7减。               
        TimeSpan ts = new TimeSpan(i, 0, 0, 0); return CurDateTime.Add(ts);
    }
    public void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("MeetManager.aspx");
    }

}
